package licode.unisop.log.service.infrastructure.repository.service;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import licode.unisop.log.client.api.LoginLogApi;
import licode.unisop.log.client.dto.LoginLogDTO;
import licode.unisop.log.client.req.LoginLogIn;
import licode.unisop.log.service.infrastructure.assembler.LogAssembler;
import licode.unisop.log.service.infrastructure.repository.mp.mapper.LoginLogMapper;
import licode.unisop.log.service.infrastructure.repository.po.LoginLogPO;
import licode.unisop.types.util.IdGenUtil;
import licode.unisop.unilite.dp.PageData;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

/**
 * 实现登录日志的查询接口
 *
 * @author WR
 * @since 2016/7/9 16:04
 */
@Primary
@Component
public class LoginLogApiImpl
        extends ServiceBase<LoginLogMapper, LoginLogPO>
        implements LoginLogApi {
    @Resource
    private LogAssembler logAssembler;

    @Override
    public void save(LoginLogDTO source) {
        LoginLogPO loginLogPo = logAssembler.fromLoginLogDto(source);
        loginLogPo.setId(IdGenUtil.buildLoginLogId());
        super.save(loginLogPo);
    }

    @Override
    public void saveList(List<LoginLogDTO> source) {
        List<LoginLogPO> loginLogs = logAssembler.fromLoginLogDto(source);
        for (LoginLogPO item: loginLogs) {
            item.setId(IdGenUtil.buildLoginLogId());
            super.save(item);
        }
    }

    @Override
    public PageData<LoginLogDTO> query(LoginLogIn req) {
        Page<LoginLogPO> pageReq = new Page<>(req.getCurrent(),
                req.getPageSize());
        pageReq.setSearchCount(true);
        Page<LoginLogPO> page = super.lambdaQuery()
                .eq(StrUtil.isNotBlank(req.getPlatformId()),
                        LoginLogPO::getPlatformId, req.getPlatformId())
                .in(null != req.getPlatformIds() && !req.getPlatformIds().isEmpty(),
                        LoginLogPO::getPlatformId, req.getPlatformIds())
                .eq(StrUtil.isNotBlank(req.getUserId()),
                        LoginLogPO::getUserId, req.getUserId())
                .eq(StrUtil.isNotBlank(req.getState()),
                        LoginLogPO::getState, req.getState())
                .like(StrUtil.isNotBlank(req.getClientId()),
                        LoginLogPO::getClientId, req.getClientId())
                .like(StrUtil.isNotBlank(req.getUserAccount()),
                        LoginLogPO::getUserAccount, req.getUserAccount())
                .like(StrUtil.isNotBlank(req.getUserName()),
                        LoginLogPO::getUserName, req.getUserName())
                .like(StrUtil.isNotBlank(req.getPlatformName()),
                        LoginLogPO::getPlatformName, req.getPlatformName())
                .le(null != req.getEndDate(),
                        LoginLogPO::getLoginTime, req.getEndDate())
                .ge(null != req.getStartDate(),
                        LoginLogPO::getLoginTime, req.getStartDate())
                .orderByDesc(LoginLogPO::getLoginTime)
                .page(pageReq);

        return convertPage(page, (List<LoginLogPO> data) ->
                logAssembler.fromLoginLog(data));
    }
}
